<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//用户表
class Users_model extends CI_Model{
    function __construct() {
        parent::__construct();
        $this->table = 'users';
        $this->users_info = 'users_info';
        $this->users_spread = 'users_spread';
        $this->account = 'account';
        $this->feedback = 'feedback';
        $this->users_line = 'users_line';
    }
    
    function get($uid){
        $this->db->select($this->table.'.*,'.$this->users_info.'.*,'.$this->account.'.*');
        $this->db->from($this->table);
        $this->db->join($this->users_info, $this->users_info.'.user_id = '.$this->table.'.user_id', 'left');
        $this->db->join($this->account, $this->account.'.user_id = '.$this->table.'.user_id', 'left');
        $this->db->where($this->table.'.user_id',$uid);
        return $this->db->get()->row_array();
    }
    
    function get_list($where,$page,$limit,$like=""){
        $this->db->select($this->table.'.*,');
        $this->db->from($this->table);
        if($like){
            $this->db->like($this->table.'.mobile',$like);
        }
        $this->db->where($where);
        $this->db->order_by('reg_time desc');
        $this->db->limit($limit, $page);
        return $this->db->get()->result_array();
    }
    function get_count($where,$like){
        return $this->db->where($where)->like($this->table.'.mobile',$like)->get($this->table)->num_rows();
    }
    
    function get_user_by_uid($uid=0){
        return $this->db->select('*')->from($this->table)->where('user_id',$uid)->order_by('reg_time desc')->get()->row_array();
    }
    
    function get_user_by_username($username){
        return $this->db->select('*')->from($this->table)->where('username',$username)->order_by('reg_time desc')->get()->row_array();
    }
    
    function get_user_by_mobile($mobile){
        return $this->db->select('*')->from($this->table)->where('mobile',$mobile)->order_by('reg_time desc')->get()->row_array();
    }
    
    function get_user_by_email($email){
        return $this->db->select('*')->from($this->table)->where('email',$email)->order_by('reg_time desc')->get()->row_array();
    }
    
    function get_spreader($where){
        $this->db->select($this->table.'.*,');
        $this->db->from($this->table);
        $this->db->where($where);
        $this->db->order_by('reg_time desc');
        return $this->db->get()->row_array();
    }
    
    function get_spreaders_id($where){
        $this->db->select($this->users_spread.'.user_id');
        $this->db->from($this->users_spread);
        $this->db->where($where);
        $this->db->order_by('create_time desc');
        return $this->db->get()->result_array();
    }
    //通过手机前三位和后三位查找用户信息
    function get_users_by_mobile_3_to_3($mobile_first,$mobile_end){
        $sql = "select * from tan_users where mobile like '".$mobile_first."%' and mobile like '%".$mobile_end."'";
        $query = $this->db->query($sql);
        return $query->row_array();
    }
    
    function user_update($data,$uid){
        return $this->db->where('user_id',$uid)->update($this->table, $data);
    }
    
    /*******用户反馈*********/
    function get_feedback_count($where){
        return $this->db->where($where)->get($this->feedback)->num_rows();
    }
    
    function get_feedback_list($where,$page,$limit){
        $this->db->select($this->feedback.'.*,');
        $this->db->from($this->feedback);
        $this->db->where($where);
        $this->db->order_by('create_time desc');
        $this->db->limit($limit, $page);
        return $this->db->get()->result_array();
    }
    
    function get_user_line_by_mobile($mobile){
        return $this->db->select('*')->from($this->users_line)->where('mobile',$mobile)->get()->row_array();
    }
    
    //获取线下用户总数
    function get_users_line_count($where){
        return $this->db->where($where)->get($this->users_line)->num_rows();
    }
    
    function get_users_line_list($where,$page,$limit){
        $this->db->select($this->users_line.'.*,');
        $this->db->from($this->users_line);
        $this->db->where($where);
        $this->db->order_by('user_id desc');
        $this->db->limit($limit, $page);
        return $this->db->get()->result_array();
    }
    
    //批量插入线下用户数据
    function insert_line_users($datas){
        $this->db->insert_batch($this->users_line, $datas);
    }
}
